package Router

import (
	"Ahq/Controllers"
	"Ahq/Middlewares"
	"Ahq/Sessions"
	"net/http"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func InitRouter(port string) {
	gin.SetMode(gin.DebugMode)
	router := gin.Default()
	router.StaticFS("/assets", http.Dir("webapp/asserts"))
	// 要在路由组之前全局使用「跨域中间件」, 否则OPTIONS会返回404
	router.Use(Middlewares.Cors())
	// 使用 session(cookie-based)
	router.Use(sessions.Sessions("myyyyysession", Sessions.Store))
	router.LoadHTMLGlob("webapp/pages/**/*")
	router.GET("/", Controllers.IndexPage)
	router.GET("/download", Controllers.Download)
	router.POST("/createPdf", Controllers.CreatePdf)
	router.POST("/preview", Controllers.Preview)

	router.Run(port)
}
